package jsyf.oa.service.device;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.device.BrandBankDao;
import jsyf.oa.dao.device.DeviceBrandDao;
import jsyf.oa.entity.device.BrandBankEntity;
import jsyf.oa.entity.device.DeviceBrandEntity;
import jsyf.oa.facade.device.DeviceBrandFacade;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jjx on 2018/3/8.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class DeviceBrandService implements DeviceBrandFacade {

    @Autowired
    private DeviceBrandDao deviceBrandDao;
    @Autowired
    private BrandBankDao brandBankDao;

    @Override
    public JSONObject getBrandList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        //品牌名称
        String brandName = jsonParams.getString("brandName");

        //分页信息
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;

        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(brandName)) {
            condition.append(" AND BRAND_NAME=?");
            sqlArgs.add(brandName);
        }
        condition.append("AND DEL_FLAG=0");
        //查询list
        List<JSONObject> records = deviceBrandDao.getJSONListByPage(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = deviceBrandDao.getPageCount(condition.toString(), sqlArgs.toArray());

        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }


    /*  @Override
      public JSONObject getByName(String jsonData) {
          JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
          String brandName = jsonParams.getString("brandName");
          List<JSONObject> records=deviceBrandDao.getByName(brandName);
          JSONObject result = new JSONObject();
          result.put("records", records);
          return result;
      }*/
    @Override
    public Boolean update(String jsonData) {
        DeviceBrandEntity deviceBrandEntity = JSON.toJavaObject(JSON.parseObject(jsonData), DeviceBrandEntity.class);
        return deviceBrandDao.update(deviceBrandEntity) > 0;
    }

    @Override
    public String updateList(String jsonData) {
        List<DeviceBrandEntity> entity = (List<DeviceBrandEntity>) JSONArray.toCollection(JSONArray.fromObject(jsonData), DeviceBrandEntity.class);
        if (entity.size()>0){
                for (int i = 0; i < entity.size(); i++) {
                    DeviceBrandEntity model = entity.get(i);
                    if (!StringUtils.isBlank(model.getId())){
                        deviceBrandDao.update(model);
                    }
                }
            }
       return "true";
    }

    @Override
    public Boolean delById(String jsonData) {
        return deviceBrandDao.delById(JSON.parseObject(jsonData).getString("id")) > 0;
    }

    @Override
    public Boolean removeById(String jsonData) {
        return deviceBrandDao.removeById(JSON.parseObject(jsonData).getString("id")) > 0;
    }


    @Override
    public String add(String jsonData) {
        JSONObject JSONObject = JSON.parseObject(jsonData);
        DeviceBrandEntity deviceBrandEntity = JSON.toJavaObject(JSONObject, DeviceBrandEntity.class);
        deviceBrandEntity.setDelFlag(0);
        deviceBrandEntity.setSortNo(0);
        deviceBrandDao.add(deviceBrandEntity);
        return deviceBrandEntity.getId();
    }

    /*获取设备类型*/
    @Override
    public JSONObject getDeviceTypeList(String jsonData) {
        JSONObject object = JSON.parseObject(jsonData);
        String companyId = object.getString("companyId");
        String brandId = object.getString("brandId");
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> devicetypes = deviceBrandDao.getDeviceTypeList(companyId, brandId);
        jsonObject.put("devicetypes", devicetypes);
        return jsonObject;
    }

    @Override
    public JSONObject getBrandList2() {
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> brands = deviceBrandDao.getBrandList2();
        jsonObject.put("brands", brands);
        return jsonObject;
    }

    @Override
    public JSONObject getBrandPage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String brandName = jsonParams.getString("brandName");
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(brandName)) {
            if (!"品牌".equals(brandName)) {
                condition.append(" AND t_dvc_brand.BRAND_NAME=?");
                sqlArgs.add(brandName);
            }
        }
        condition.append(" AND t_dvc_brand.DEL_FLAG = 0 order by SORT_NO desc");
        //查询list
        List<JSONObject> records = deviceBrandDao.getBrandList(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        JSONObject root = new JSONObject();
        root.put("name", "品牌");
        root.put("type", "root");
        root.put("open", "true");
        records.add(root);
        Integer recordCount = deviceBrandDao.getPageCount1(condition.toString(), sqlArgs.toArray());
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getBrandPage2(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String brandName = jsonParams.getString("brandName");
        int requestPage = 1;
        int pageSize = Integer.MAX_VALUE;
        if (jsonParams.containsKey("pageSize")) {
            pageSize = jsonParams.getInteger("pageSize");
        }
        if (jsonParams.containsKey("requestPage")) {
            requestPage = jsonParams.getInteger("requestPage");
        }
        //传参
        StringBuffer condition = new StringBuffer();
        List<Object> sqlArgs = new ArrayList<Object>();
        if (!StringUtils.isBlank(brandName)) {
            if (!"品牌".equals(brandName)) {
                condition.append(" AND t_dvc_brand.BRAND_NAME=?");
                sqlArgs.add(brandName);
            }
        }
        condition.append(" AND t_dvc_brand.DEL_FLAG = 0 order by SORT_NO desc");
        //查询list
        List<JSONObject> records = deviceBrandDao.getBrandList(condition.toString(), sqlArgs.toArray(), requestPage, pageSize);
        //查询总数
        Integer recordCount = deviceBrandDao.getPageCount1(condition.toString(), sqlArgs.toArray());
        //分装前端需要的格式
        int pageCount = 0;
        if (recordCount < pageSize) {
            pageCount = 1;
        } else {
            double flag = Math.ceil(recordCount / pageSize + 1);
            pageCount = ((int) flag);
        }
        if (recordCount == 0) {
            pageCount = 0;
        }
        //存放分页信息
        JSONObject sendData = new JSONObject();
        //存放list
        JSONObject result = new JSONObject();
        sendData.put("recordCount", recordCount);
        sendData.put("pageCount", pageCount);
        sendData.put("requestPage", 1);
        result.put("pager", sendData);
        // 子数组对象
        result.put("records", records);
        return result;
    }

    @Override
    public JSONObject getBrandById(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String id = jsonParams.getString("id");
        return deviceBrandDao.getJSONById(id);
    }

    @Override
    public JSONObject getBrandByCompanyId(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        JSONObject obj = new JSONObject();
        obj.put("brands", deviceBrandDao.getBrandByCompanyId(companyId));
        return obj;
    }

    @Override
    public JSONObject getBankList(String jsonData) {
        List<JSONObject> banks = brandBankDao.getBankList();
        JSONObject obj = new JSONObject();
        obj.put("banks", banks);
        return obj;
    }

    @Override
    public String addBrandBank(String jsonObject) {
        brandBankDao.removeByCondition("", new String[]{});
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonObject, JSONObject.class);
        List<JSONObject> brandBankList = FastJsonConvert.convertJSONToArray(jsonParams.getString("banks"), JSONObject.class);
        for (JSONObject brandBank : brandBankList) {
            BrandBankEntity brandBankEntity = JSON.toJavaObject(brandBank, BrandBankEntity.class);
            brandBankEntity.setDelFlag(0);
            brandBankDao.add(brandBankEntity);
        }
        return "添加成功";
    }

    @Override
    public List<JSONObject> getBankAndAccount(String jsonData) {
        JSONObject object = JSON.parseObject(jsonData);
        String brandId=object.getString("brandId");
        StringBuffer condition=new StringBuffer();
        condition.append(" AND BRAND_ID=?");
        return brandBankDao.getBankAndAccount(condition.toString(),new Object[]{brandId});
    }

    @Override
    public JSONObject getBankAndAccountById(String jsonData) {
        JSONObject object = JSON.parseObject(jsonData);
        String brandId=object.getString("brandId");
        String bankName=object.getString("bankName");
        StringBuffer condition=new StringBuffer();
        condition.append(" AND BRAND_ID=? AND BANK_NAME=?");
        return brandBankDao.getBankAndAccountById(condition.toString(),new Object[]{brandId,bankName});
    }
}
